package com.sy.simpleegg.ui.horizontalrecyclerview.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sy.simpleegg.constant.Constant.CLICK_INTERVAL
import com.sy.simpleegg.databinding.ItemHomeHorizontalGoodsBinding
import com.sy.simpleegg.ext.logFile
import com.sy.simpleegg.room.entity.GoodEntity
import com.sy.simpleegg.ui.horizontalrecyclerview.adapter.HorizontalAdapter.GoodHolder

class HorizontalAdapter(private var goodList: MutableList<GoodEntity>?, private var onItemClickListener: OnItemClickListener?) :
    RecyclerView.Adapter<GoodHolder>() {

    private var showChannel:Boolean  = false

    private var lastClickTime = 0L

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodHolder {
        val binding = ItemHomeHorizontalGoodsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return GoodHolder(binding)
    }

    override fun getItemCount(): Int = goodList?.size ?: 0

    override fun onBindViewHolder(holder: GoodHolder, position: Int) {
        try {
            logFile("${javaClass.simpleName} onBindViewHolder")
//            goodList?.let {
//                val goodDataBean = it[position]
//                Glide.with(holder.itemView)
//                    .load(goodDataBean.imgUrl)
//                    .fitCenter()
//                    .into(holder.goodImg)
//                holder.channel.visibility = if (showChannel) View.VISIBLE else View.GONE
//                holder.channel.text = goodDataBean.channel.toString()
//                holder.goodName.isSelected = true
//                holder.goodName.text = goodDataBean.goodName
//                holder.goodPrice.text = BigDecimalUtil.getPrice(goodDataBean.price)
//                holder.goodAdd.setOnClickListener {
//                    if (!isFirstClick()) {
//                        return@setOnClickListener
//                    }
//                    onItemClickListener?.addGood(goodDataBean)
//                }
//                holder.goodImg.setOnClickListener {
//                    if (!isFirstClick()) {
//                        return@setOnClickListener
//                    }
//                    onItemClickListener?.showGoodDetails(goodDataBean)
//                }
//                holder.goodName.setOnClickListener {
//                    if (!isFirstClick()) {
//                        return@setOnClickListener
//                    }
//                    onItemClickListener?.showGoodDetails(goodDataBean)
//                }
//            }
        }catch (e: Exception){
            e.printStackTrace()
            logFile("${javaClass.simpleName} 商品数据处理异常：${e.message}")
        }
    }

    private fun isFirstClick(): Boolean{
        val currentClickTime = System.currentTimeMillis()
        val canClick = currentClickTime - lastClickTime >= CLICK_INTERVAL
        lastClickTime = currentClickTime
        return canClick
    }

    fun showChannelNumber(show: Boolean){
        showChannel = show
    }

    interface OnItemClickListener{
        fun addGood(goodDataBean: GoodEntity)
        fun showGoodDetails(goodDataBean: GoodEntity)
    }

    inner class GoodHolder(binding: ItemHomeHorizontalGoodsBinding): RecyclerView.ViewHolder(binding.root){
        var goodImg: ImageView = binding.ivGood
        var goodName: TextView = binding.tvGoodName
        var channel: TextView = binding.tvChannel
        var goodPrice: TextView = binding.tvGoodPrice
        var goodAdd: ImageView = binding.ivAdd
    }

}
